package com.weiiew.buildservice.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.feignapi.client.RecipeClient;
import com.weiiew.buildservice.annotation.BuildChangeCacheEvict;
import com.weiiew.buildservice.mapper.BuildingCostMapper;
import com.weiiew.buildservice.mapper.BuildingMapper;
import com.weiiew.common.entity.dto.Building;
import com.weiiew.common.entity.dto.BuildingCost;
import com.weiiew.common.entity.qo.BuildingQo;
import com.weiiew.common.entity.simple.NumberPair;
import com.weiiew.common.entity.simple.SimpleBuilding;
import com.weiiew.common.entity.simple.SimpleItem;
import com.weiiew.common.entity.simple.SimpleType;
import com.weiiew.common.entity.vo.BuildingVO;
import com.weiiew.common.exception.messageException;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 魏锴
 * @description 针对表【building】的数据库操作Service实现
 * @createDate 2024-02-09 17:56:29
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class BuildingServiceImpl implements BuildingService {
    private final BuildingMapper baseMapper;
    private final BuildingCostMapper buildingCostMapper;
    private final RecipeClient recipeClient;

    @Override
    @BuildChangeCacheEvict
    public boolean insertBuilding(Building building, Integer id, Integer version) {
        List<NumberPair> costs = building.getCosts();
        baseMapper.insert(building);
        baseMapper.setIcon(building.getId());
        for (NumberPair cost : costs){
            BuildingCost c = new BuildingCost(null, building.getId(), cost.getId(), cost.getNumber());
            buildingCostMapper.insert(c);
        }
        return true;
    }
    @Override
    @BuildChangeCacheEvict
    public boolean updateBuilding(Building building, Integer id, Integer version) {
        List<NumberPair> costs = building.getCosts();
        building.setCosts(null);
        //修改建筑数据
        baseMapper.updateById(building);
        //删除已有关联
        QueryWrapper<BuildingCost> wrapper = new QueryWrapper<BuildingCost>().eq("building_id", building.getId());
        buildingCostMapper.delete(wrapper);
        costs.forEach((cost) -> {
            BuildingCost c = new BuildingCost(null, building.getId(), cost.getId(), cost.getNumber());
            buildingCostMapper.insert(c);
        });
        return true;
    }

    @Override
    @BuildChangeCacheEvict
    public boolean removeBuilding(Integer id, Integer version) throws messageException {
        long number = recipeClient.countRecipeBuild(id);// 删除RecipeBuilding表数据
        if (number != 0) throw new messageException("该建筑仍有配方在使用");
        QueryWrapper<BuildingCost> wrapper2 = new QueryWrapper<BuildingCost>().eq("building_id", id);
        buildingCostMapper.delete(wrapper2);
        int f = baseMapper.deleteById(id);//删除Building表数据
        return f > 0;
    }

    @Override
    @Cacheable(cacheNames = "build:list:type:simple", key = "#version")
    public List<SimpleType> getBuildingTypes(Integer version) {
        return baseMapper.getBuildingTypes(version);
    }

    @Override
    @Cacheable(cacheNames = "build:list:cost", key = "#version")
    public List<SimpleItem> getBuildingCosts(Integer version) {
        return buildingCostMapper.getBuildingCosts(version);
    }

    @Override
    @Cacheable(cacheNames = "build:one", key = "#id")
    public Building getBuildingById(Integer id) {
        return baseMapper.getBuildingById(id);
    }

    @Override
    @Cacheable(cacheNames = "build:list", key = "#qo.hasPageKey")
    public List<BuildingVO> getBuildingList(BuildingQo qo) {
        return baseMapper.getBuildingList(qo);
    }

    @Override
    @Cacheable(cacheNames = "build:length", key = "#qo.noPageKey")
    public Integer getBuildingLength(BuildingQo qo) {
        return baseMapper.getBuildingLength(qo);
    }
    @Override
    @Cacheable(cacheNames = "build:list:simple", key = "#qo.noPageKey")
    public List<SimpleBuilding> getSimpleBuildingList(BuildingQo qo) {
        return baseMapper.getSimpleBuildingList(qo);
    }

    @Override
    @Cacheable(cacheNames = "build:vo", key = "#id")
    public BuildingVO getDetailBuild(Integer id) {
        return baseMapper.getDetailBuild(id);
    }

    @Override
    @Cacheable(cacheNames = "build:list:cost", key = "#costId")
    public List<BuildingVO> getBuildingsByCostId(Integer costId) {
        return baseMapper.getBuildingsByCostId(costId);
    }

    @Override
    @Cacheable(cacheNames = "build:list:icon", key = "#version")
    public List<String> getBuildIconList(Integer version) {
        return baseMapper.getBuildIconList(version);
    }
}




